﻿package ua.mihaylenko.canvas.gui 
{
	import com.hybrid.ui.ToolTip;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.filters.BlurFilter;
	import flash.geom.Rectangle;
	import flash.net.navigateToURL;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.text.TextFormat;
	import flash.utils.setTimeout;
	import gs.TweenMax;
	import ua.mihaylenko.canvas.common.DocumentClass;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import ua.mihaylenko.canvas.controller.CanvasController;
	import ua.mihaylenko.canvas.controller.CanvasEvent;
	import ua.mihaylenko.canvas.controller.EventManager;
	import ua.mihaylenko.canvas.data.GlobalSettings;
	import ua.mihaylenko.canvas.data.DataManager;
	import ua.mihaylenko.canvas.gui.dscene.GUI3ds;
	import ua.mihaylenko.canvas.gui.photoshop.GUIPhotoshop;
	import ua.mihaylenko.canvas.utils.Preloader;
	/**
	 * ...
	 * @author Mihayelnko A.L.
	 */
	public class GUIMain extends GUISimpleWindow
	{
		public var _sbtLoadImage:SimpleButton;
		public var btnFinish:SimpleButton;

		public var _sbtPrice:SimpleButton;
		public var _sbtHelp:SimpleButton;
		
		public var _cPhotoShop:GUIPhotoshop;
		public var mcWindow:MovieClip;
		public var _smc3dRenderer:GUI3ds;
		private var _bEnabled:Boolean;
		private var tt:ToolTip;
		
		private var _smcEnabled:Sprite;
		private var tf:TextFormat;
		public static var THIS:GUIMain
		private var xmlSendLoad:URLLoader;
		private var xmlURLReq:URLRequest;
		private var trips:XML;
		public function GUIMain() 
		{
			THIS = this;
			_bEnabled = true;
			//_sbtLoadImage.useHandCursor = false;
			_sbtPrice.useHandCursor = false;
			_sbtHelp.useHandCursor = false;
			_sbtLoadImage.addEventListener(MouseEvent.CLICK, clickOnLoadImage);
			btnFinish.addEventListener(MouseEvent.CLICK, clickFinish);
			_smcEnabled = new Sprite();
			_smcEnabled.graphics.beginFill(0xFFFFFF, 0.5);
			_smcEnabled.graphics.drawRect(0, 0, DocumentClass.AppStage.stageWidth, DocumentClass.AppStage.stageHeight);
			_smcEnabled.graphics.endFill();
			EventManager.getInstance().addEventListener(CanvasEvent.ENABLED_OFF, disabled);
			EventManager.getInstance().addEventListener(CanvasEvent.ENABLED_ON, enabled);
			EventManager.getInstance().addEventListener(CanvasEvent.VIEW_3D, show3D);
			EventManager.getInstance().addEventListener(CanvasEvent.VIEW_2D, show2D);
			_sbtHelp.addEventListener(MouseEvent.MOUSE_OVER, showHelpToolTip);
			_sbtPrice.addEventListener(MouseEvent.MOUSE_OVER, showPriceToolTip);
			_sbtHelp.addEventListener(MouseEvent.MOUSE_OUT, hideToolTip);
			_sbtPrice.addEventListener(MouseEvent.MOUSE_OUT, hideToolTip);
			
			tf = new TextFormat();
			tf.bold = true;
			tf.size = 12;
			tf.color = 0xff0000;
			 
			tt = new ToolTip();
			tt.hook = true;
			tt.hookSize = 20;
			tt.cornerRadius = 20;
			tt.align = "center";
			tt.titleFormat = tf;
			mcWindow.mouseEnabled = false;
			mcWindow.mouseChildren = false;
			checkFinishButton();
			
		}
		
		private function show2D(e:CanvasEvent):void 
		{
			_sbtLoadImage.visible = true;
			btnFinish.visible = true;
		}
		private function show3D(e:CanvasEvent):void 
		{
			_sbtLoadImage.visible = false;
			btnFinish.visible = false;
		}
		public function checkFinishButton():void
		{
			if (CanvasController.FILE_LOADED) btnFinish.visible = true;
			else btnFinish.visible = !true;
		}
		private function clickFinish(e:Event):void 
		{
			btnFinish.enabled = false;
			btnFinish.visible = false;
			_sbtHelp.enabled = false;
			_sbtPrice.enabled = false;
			_sbtLoadImage.enabled = false;
			
			trips = 
			<data>
				<numItems/>
				<pathToImage/>
				<pathToPreview/>
				<zoom/>
				<rotation/>
				<imageX/>
				<imageY/>
				<filter/>
				<boxWidth/>
				<boxHeight/>
				<boxDepth/>
				<color/>
			</data>;
			
			trips.numItems.@value = DataManager.getInatsnce().numItems;
			trips.pathToImage.@value = DataManager.getInatsnce().uploadDir+DataManager.getInatsnce().fileName;
			trips.pathToPreview.@value = DataManager.getInatsnce().uploadDir+DataManager.getInatsnce().filePrevName;
			
			trips.zoom.@value = DataManager.getInatsnce().zoom;
			trips.rotation.@value = DataManager.getInatsnce().rotation;
			trips.imageX.@value = DataManager.getInatsnce().imageX;
			trips.imageY.@value = DataManager.getInatsnce().imageY;
			trips.filter.@value = DataManager.getInatsnce().filter;
			trips.boxWidth.@value = DataManager.getInatsnce().boxWidth;
			trips.boxHeight.@value = DataManager.getInatsnce().boxHeight;
			trips.boxDepth.@value = DataManager.getInatsnce().boxDepth/3;
			trips.color.@value = DataManager.getInatsnce().color;
			
			
			_smcEnabled.graphics.clear();
			_smcEnabled.graphics.beginFill(0xFFFFFF, 0);
			_smcEnabled.graphics.drawRect(0, 0, DocumentClass.AppStage.stageWidth, DocumentClass.AppStage.stageHeight);
			_smcEnabled.graphics.endFill();
			_smcEnabled.buttonMode = true;
			_smcEnabled.useHandCursor = false;
			addChild(_smcEnabled);
			mcWindow.x = 0;
			mcWindow.visible = false;
			trace(root.width, root.height);
			trace(root.width, root.height);
			setTimeout(encode,150);
		}
		
		private function encode():void
		{
			var screenShot:Screenshot = new Screenshot();
			var dObj:Sprite = new Sprite();
			var bmd:BitmapData = new BitmapData(800, 550);
			bmd.draw(root, null, null, null, new Rectangle(0, 0, 800, 550));
			
			var bitmap:Bitmap = new Bitmap(bmd);
			
			addChild(dObj);
			dObj.addChild(bitmap);
			screenShot.createBitmap(dObj);	
		}
		
		public function navigate():void
		{
			
			xmlSendLoad = new URLLoader();
			var date:Date = new Date();
			var urlVar:URLVariables = new URLVariables();
			urlVar.mess = trips.toString();
			urlVar.file = "file_" + date.fullYear +"_" + date.month + "_" + date.day;
			trace(urlVar.mess);
			xmlURLReq = new URLRequest(DataManager.getInatsnce().uploadDir+DataManager.getInatsnce().sFinishApp);
			xmlURLReq.data = urlVar;
			xmlURLReq.method = URLRequestMethod.POST
			
			navigateToURL(xmlURLReq, "_self");
			
		}
		private function onIOError(e:IOErrorEvent):void 
		{
			Trace.info("onIOError " +e.toString());
		}
		
		private function onCompleteSendToServer(e:Event):void 
		{
			Trace.info("onCompleteSendToServer")
			Preloader.getInstance().progress = 1;
			Preloader.getInstance().label = "complete";
			visible = false;
		}
		
		private function showHelpToolTip(e:MouseEvent):void 
		{
			TweenMax.killAll();
			TweenMax.killAllTweens();
			TweenMax.killAllDelayedCalls(true);
			mcWindow["txtTittle"].text = "Help";
			mcWindow["txtText"].htmlText = DataManager.getInatsnce().helpText;
			TweenMax.to(mcWindow, 0.4, { x:mouseX+20, y:mouseY+20 } );
		}
		
		private function showPriceToolTip(e:MouseEvent):void 
		{
			TweenMax.killAll();
			TweenMax.killAllTweens();
			TweenMax.killAllDelayedCalls(true);
			mcWindow["txtTittle"].text = "Price";
			mcWindow["txtText"].htmlText = DataManager.getInatsnce().priceText;
			TweenMax.to(mcWindow, 0.4, { x:mouseX+20, y:mouseY+20 } );
		}
		private function hideToolTip(e:MouseEvent):void 
		{
			TweenMax.to(mcWindow, 0.2, { delay:0.5, x:Math.random()*500, y:-500} );
		}
		private function clickOnLoadImage(event:MouseEvent = null):void {
			EventManager.getInstance().dispatchEvent(new CanvasEvent(CanvasEvent.SELECT_FILE));
		}
		private function disabled(event:CanvasEvent) {
			addChild(_smcEnabled);
			filters = [new BlurFilter(GlobalSettings.BLUR_FOR_ENABLED, GlobalSettings.BLUR_FOR_ENABLED, 100)];
		}
		private function enabled(event:CanvasEvent) {
			if(this.contains(_smcEnabled)) {
				this.removeChild(_smcEnabled);
			}
			filters = null;
		}
	}
	
}